package com.eqdd.yiqidian.ui.act

import android.content.Intent
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.WriteBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.*
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.kotlin.BaseAddActivity
import com.eqdd.yiqidian.kotlin.areaSelect
import com.eqdd.yiqidian.kotlin.bottomDialog
import com.eqdd.yiqidian.kotlin.timePicker
import com.eqdd.yiqidian.kotlin.utils.TimePickerUtil
import com.eqdd.yiqidian.ui.WriteActivity
import com.eqdd.yiqidian.ui.act.tree.ActTypeTreeActivity
import com.eqdd.yiqidian.utils.HttpUtil
import com.eqdd.yiqidian.utils.ImageUtil
import com.eqdd.yiqidian.utils.PicUtil
import com.eqdd.yiqidian.widget.ChooseAlertView
import com.kyleduo.switchbutton.SwitchButton
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.lzy.okgo.model.HttpParams
import kotlinx.android.synthetic.main.layout_image_selected.*
import org.jetbrains.anko.startActivityForResult
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by jhj on 18-9-29.
 */
class ActAddActivity : BaseAddActivity() {

    private val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
    private var isFree = true

    override val title: String
        get() = "活动"
    override val url: String
        get() = HttpConfig.BASE_URL + HttpConfig.ACTIVITY_ADD

    override fun uploadParams(): HttpParams {
        val params = HttpParams()
        val bean = slimAdapterEx.data
        val location = bean[8].tag as String
        if (location.contains(",")) {
            val array = location.split(",")
            if (array.size == 3) {
                params.put("actProvince", array[0])
                params.put("actCity", array[1])
                params.put("actAddress", array[2])
            }
        }
        params.put("actTitle", bean[1].content)
        params.put("actType", bean[2].content)
        params.put("actClassify", bean[3].content)
        params.put("actNum", bean[4].content)
        params.put("actScale", bean[5].content)
        params.put("actStartTime", bean[6].content)
        params.put("actEndTime", bean[7].content)

        params.put("actImg", bean[10].tag as String?)
        params.put("actDesc", bean[11].tag as String?)
        params.put("actSchedule", bean[12].tag as String?)

        params.put("ischeck", ((bean[14] as? SlimEditCheckButtonBean)?.isCheck == true))
        params.put("actObject", bean[15].content)

        params.put("isCharge", ((bean[17] as? SlimEditPriceBean)?.isCheck == true))
        params.put("price", bean[18].content ?: "0")

        if (isFree) {
            params.put("name", bean[19].content)
            params.put("department", bean[20].content)
            params.put("post", bean[21].content)
            params.put("phone", bean[22].content)
            params.put("email", bean[24].content)
        } else {
            params.put("name", bean[20].content)
            params.put("department", bean[21].content)
            params.put("post", bean[22].content)
            params.put("phone", bean[23].content)
            params.put("email", bean[25].content)
        }

        isNeedCompany = !isFree

        return params
    }

    override fun itemClick(p: Int) {
        when (p) {
            2 -> {
                val list = arrayListOf("论坛", "大会", "线上直播", "点播", "研讨会", "培训", "市场活动", "展览", "聚会", "酒会", "其他")
                bottomDialog(list) {
                    slimAdapterEx.data[2].content = list[it - 1]
                    slimAdapterEx.notifyItemChanged(2)
                }
            }
            3 -> {
                startActivityForResult<ActTypeTreeActivity>(3)
            }
            6 -> {
                val startTime = slimAdapterEx.getDataItem(6).content
                val endTime = slimAdapterEx.getDataItem(7).content
                timePicker(TimePickerUtil.parseData(startTime, sdf), TimePickerUtil.parseData(endTime, sdf), TimePickerUtil.type2, true, body = {
                    slimAdapterEx.getDataItem(6).content = sdf.format(it)
                    slimAdapterEx.notifyItemChanged(6)
                })
            }
            7 -> {
                val startTime = slimAdapterEx.getDataItem(6).content
                val endTime = slimAdapterEx.getDataItem(7).content
                timePicker(TimePickerUtil.parseData(startTime, sdf), TimePickerUtil.parseData(endTime, sdf), TimePickerUtil.type2, false, body = {
                    slimAdapterEx.getDataItem(7).content = sdf.format(it)
                    slimAdapterEx.notifyItemChanged(7)
                })
            }
            8 -> {
                areaSelect { province, city, district ->
                    ChooseAlertView.getEditDiglog("请输入详细地址", "详细地址", null) { contents ->
                        slimAdapterEx.getDataItem(8).content = province + city + district + contents[0]
                        slimAdapterEx.getDataItem(8).tag = "$province,$city,$district${contents[0]}"
                        slimAdapterEx.notifyItemChanged(8)
                    }.show(supportFragmentManager, "areaDialog")
                }
            }

            11 -> startActivityForResult<WriteActivity>(11, Config.TITLE to "活动描述", "isHasTitle" to false,
                    "content" to slimAdapterEx.getDataItem(11).tag as String?)
            12 -> startActivityForResult<WriteActivity>(12, Config.TITLE to "活动日程", "isHasTitle" to false,
                    "content" to slimAdapterEx.getDataItem(12).tag as String?)
            17 -> {
                val list = arrayListOf<String>("免费", "收费")
                bottomDialog(list) {
                    if (it == 2) {
                        ChooseAlertView.getEditDiglog("请输入费用", "请输入", null) { contents ->
                            slimAdapterEx.getDataItem(17).content = contents[0]
                            slimAdapterEx.notifyItemChanged(17)
                        }.show(supportFragmentManager, "priceDialog")
                    }
                }
            }
        }

        if (p == 25 && isFree) {
            val list = arrayListOf<String>("个人", "企业")
            bottomDialog(list) {
                slimAdapterEx.data[25].content = list[it - 1]
                slimAdapterEx.notifyItemChanged(25)
            }
        } else if (p == 26) {
            val list = arrayListOf<String>("个人", "企业")
            bottomDialog(list) {
                slimAdapterEx.data[26].content = list[it - 1]
                slimAdapterEx.notifyItemChanged(26)
            }
        }
    }

    override fun setEditBeans() {
        datas.add(SlimEditOtherBean("基本信息", 0))
        datas.add(SlimEditEnterBean("活动主题", "", 1, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("活动类型", "", 2, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("活动分类", "", 3, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("活动人数", "", 4, "请选择").setInteger(true).setNeed(true))
        datas.add(SlimEditEnterBean("活动规模", "", 5, "请选择").setNeed(true))
        datas.add(SlimEditEnterBean("开始时间", "", 6, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("结束时间", "", 7, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("活动地址", "", 8, "请选择").setNeed(true).setDeal(true))
        datas.add(SlimEditOtherBean("活动内容", 9))
        datas.add(SlimImageSelectBean("活动封面图", 10, "").setNeed(true))
        datas.add(SlimEditEnterBean("活动描述", "", 11, "请输入").setNeed(true).setDeal(true))
        datas.add(SlimEditEnterBean("活动日程", "", 12, "请输入").setNeed(true).setDeal(true))
        datas.add(SlimEditOtherBean("报名", 13))
        datas.add(SlimEditCheckButtonBean("审核参会资格", "", 14, false))
        datas.add(SlimEditEnterBean("参会对象", "", 15, "请输入").setNeed(true))
        datas.add(SlimEditOtherBean("费用", 16))
        datas.add(SlimEditPriceBean("是否收费", "", 17, false))
        datas.add(SlimEditOtherBean("联系人信息", 19))
        datas.add(SlimEditEnterBean("姓名", user.username, 20, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("部门", user.department, 21, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("职位", user.post, 22, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("手机", user.uname, 23, "请输入").setInteger(true).setNeed(true))
        datas.add(SlimEditEnterBean("固话", "", 24, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("邮箱", "", 25, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("身份", "", 26, "请选择").setInteger(true).setNeed(true).setDeal(true))

        slimAdapterEx.register(R.layout.item_edit_title_type, SlimInjector<SlimEditOtherBean> { data, injector ->
            injector?.text(R.id.tv_top_title, data?.title)
        })
        slimAdapterEx.register(R.layout.layout_image_selected, SlimInjector<SlimImageSelectBean> { data, injector ->
            injector?.text(R.id.tv_label, data?.title)
                    ?.clicked(R.id.iv_add) {
                        PicUtil.single(this@ActAddActivity, true)
                    }
        })
        slimAdapterEx.register(R.layout.list_item_24, SlimInjector<SlimEditCheckButtonBean> { data, injector ->
            injector?.text(R.id.tv_left_center_content, data?.title)
                    ?.checked(R.id.sb_end, data?.isCheck == true)
                    ?.with<SwitchButton>(R.id.sb_end) {
                        it.setOnCheckedChangeListener { view, isChecked ->
                            (slimAdapterEx.getDataItem(14) as SlimEditCheckButtonBean).isCheck = isChecked
                        }
                    }

        })
        slimAdapterEx.register(R.layout.list_item_24, SlimInjector<SlimEditPriceBean> { data, injector ->
            injector?.text(R.id.tv_left_center_content, data?.title)
                    ?.checked(R.id.sb_end, data?.isCheck == true)
                    ?.with<SwitchButton>(R.id.sb_end) {
                        it.setOnCheckedChangeListener { view, isChecked ->
                            isFree = !isChecked
                            (slimAdapterEx.getDataItem(17) as SlimEditPriceBean).isCheck = isChecked
                            if (isChecked) {
                                slimAdapterEx.data.add(18, SlimEditEnterBean("费用", null, 18, "请输入").setNumber(true))
                                slimAdapterEx.notifyItemInserted(18)
                            } else {
                                slimAdapterEx.data.removeAt(18)
                                slimAdapterEx.notifyItemRemoved(18)
                            }
                        }
                    }

        })

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    val localMedias = PictureSelector.obtainMultipleResult(data)
                    ImageUtil.setImage(localMedias?.get(0)?.compressPath, iv_add)
                    val filePath = localMedias?.get(0)?.compressPath
                    HttpUtil.upLoadPic(this@ActAddActivity, filePath) {
                        slimAdapterEx.getDataItem(10).tag = it
                    }


                }
                3 -> {
                    val name = data?.getStringExtra(Config.NAME)
                    slimAdapterEx.getDataItem(3).content = name
                    slimAdapterEx.notifyItemChanged(3)
                }
            }
        }

        if (resultCode == Config.SUCCESS) {
            when (requestCode) {
                11 -> {
                    val writeBean = data?.getSerializableExtra(Config.BEAN) as WriteBean?
                    slimAdapterEx.getDataItem(11).content = "已填写"
                    slimAdapterEx.getDataItem(11).tag = writeBean?.html
                    slimAdapterEx.notifyItemChanged(11)
                }
                12 -> {
                    val writeBean = data?.getSerializableExtra(Config.BEAN) as WriteBean?
                    slimAdapterEx.getDataItem(12).content = "已填写"
                    slimAdapterEx.getDataItem(12).tag = writeBean?.html
                    slimAdapterEx.notifyItemChanged(12)
                }
            }
        }

    }

    class SlimEditPriceBean(title: String, content: String, position: Int, var isCheck: Boolean) : SlimEditBean(title, content, position)


}